//logs.js
var app = getApp();
var WxParse = require('../../wxParse/wxParse.js');
Page({
  data: {
    logs: [],
    tabId: 0,
    showMore: 0,
    userlist: [],
    userPage: 1,
    thumb_up_ed: 0,
    user_info: {},
    commentType:'general',
    imgList: [],
    recordsTotal: 0,
    commentLastpage: 0,
    topicLastpage: 0,
    loading: 0,
    limitTopic: 0,
    topicType: 0,
    comment_page: 1,//评论页数
    thumbs: [],//点赞列表 
    topicPage: 1,
    topicData: [],
    data: [],
    to_user_id: 0,
    listData: [],
    popup: 0,


  },
  onLoad: function (options) {
    this.setData({ extConfig: app.getExtConfigSync() })
    //问题详情
    var that = this;

    var id = options.id ? options.id : 2;
    if (options.scene) {
       id = decodeURIComponent(options.scene);
    }
    var user_info = wx.getStorageSync('user_info');// 83589 83586 83591 83651
    that.setData({ id: id, user_info: user_info });
    //console.log(user_info);
    // that.setData({ topicType: 4 });
    wx.showShareMenu({
      withShareTicket: true
    })
    this.init();
  },
  
  
  init: function (type = 0) {
    if (type == 1) {
      this.setData({
        commentLastpage: 0,
        topicLastpage: 0,
        loading: 0,
        topicPage: 1,
        comment_page: 1,
        topicData: [],
        listData: [],
      })
    }
    app.setPageData("topic/detail", { reload: 0 });
    var that = this;
    //evens/detail 活动详情 
    
    //events/thumbs 活动赞列表 
    console.log(this.data.topicType);
    that.getTopic(type);
      app.api('sns/topic/thumbs', {
        topic_id: that.data.id,
        order: 'id desc',
        limit: 4,
      }, function (res) {
        if (res.code != 200) {
          app.alert(res.msg);
          return false;
        }
        that.setData({ thumbs: that.data.thumbs.concat(res.data.list) });
      });
      //events/comments 活动评论 
      this.getComments(type);
  },

  getTopic(reset = 0) {
    var that = this;
    app.api('sns/topic/detail', {
      id: that.data.id
    }, function (res) {
      wx.stopPullDownRefresh();
      if (res.code != 200) {
        app.alert(res.msg);
        return false;
      }
      if (res.data.pics) {
        that.setData({ imgList: res.data.pics });
      }

      that.setData({ thumb_up_ed: Number(res.data.thumb_up_ed) + 1 });
      that.setData({ data: res.data });
      WxParse.wxParse('article', 'html', res.data.content, that, 5);
    });
  },

  getComments(reset = 0) {
    var that = this;
    var comment_page = that.data.comment_page;
    app.api('sns/comment/get', {
      pid: that.data.id,
      page: comment_page,
    }, function (res) {
      //console.log(res);
      if (res.code != 200) {
        app.alert(res.msg);
        return false;
      }
      comment_page++;
      that.setData({ comment_page: comment_page, listData: that.data.listData.concat(res.data.list) });
    });
  },

  //点选 events/thumb_up
  thumb_up() {
    var that = this;
    //console.log(user_info);
    var user_info = this.data.user_info;
    var topicData = that.data.data;
    app.thumb_up(that.data.id, function (res) {
      if (res.type == 'add') {
        var thumbs = [{
          user_id: user_info.user_id,
          nickname: user_info.nickname,
          avatarUrl: user_info.avatarUrl,
        }].concat(that.data.thumbs);
        that.setData({ thumb_up_ed: 2 });
        ++topicData.good_count;
      } else if (res.type == 'del') {
        var thumbs = that.data.thumbs;
        thumbs.splice(0, 1);
        that.setData({ thumb_up_ed: 1 });
        --topicData.good_count;
      }
      that.setData({ thumbs: thumbs, data: topicData });
    })
  },

  

  commentAct: function (e) {
    var to_user_id = e.currentTarget.dataset.user_id;
    var itemListArray = [];
    itemListArray.push({ text: '回复', act: this.to_reply });
    if (to_user_id == this.data.user_info.user_id || to_user_id == this.data.data.user_id) {
      itemListArray.push({ text: '删除', act: this.del_comment });
    }
    var itemList = [];
    //console.log(itemListArray);
    for (var i in itemListArray) {
      itemList.push(itemListArray[i].text);
    }
    wx.showActionSheet({
      itemList: itemList,
      success: function (res) {
        console.log(itemListArray[res.tapIndex]);
        itemListArray[res.tapIndex]['act'](e);
      },
      fail: function (res) {
        console.log(res.errMsg)
      }
    })
  },
  //回答
  reply: function () {
    this.setData({ 'popup': 1 });
    this.setData({ 'to_comment_id': 0, commentType: 'general' ,'to_user_id': this.data.data.user_id });
    this.setData({ 'reply': { value: '', placeholder: '这里写下你的评论...' } })
  },
  //回复
  to_reply: function (e) {
    console.log(e);
    var to_comment_id = e.currentTarget.dataset.comment_id;
    var to_nickname = e.currentTarget.dataset.nickname;
    var to_user_id = e.currentTarget.dataset.user_id;
    this.setData({ 'popup': 1 });
    this.setData({ 'to_comment_id': to_comment_id, 'to_user_id': to_user_id, commentType: 'general' });
    this.setData({ 'reply': { value: '', placeholder: '@' + to_nickname } })
  },
  dealComment: function (e) {
    this.setData({ 'popup': 1 });
    this.setData({ 'to_comment_id': 0, 'to_user_id': this.data.data.user_id, commentType:'updateStatus' });
    this.setData({ 'reply': { value: '', placeholder: '这里写下内容...' } })

  },

  //删除
  del_comment: function (e) {
    var that = this;
    var idx = e.currentTarget.dataset.key;
    var comment_id = e.currentTarget.dataset.comment_id;
    var topicData = that.data.data;
    wx.showModal({
      title: '提示',
      content: '确认删除吗？',
      success: function (res) {
        if (res.confirm) {
          //删除评论
          app.api("sns/comment/del", {
            id: comment_id,
            method: 'POST'
          }, function (res) {
            //删了出错
            if (res.code != 200) {
              app.alert(res.msg);
            } else {
              wx.showToast({
                title: '删除成功',
                icon: 'success',
                duration: 2000
              })
              var listData = that.data.listData;
              listData[idx]['none'] = 1;
              --topicData.comment_count;
              that.setData({ listData: listData, data: topicData });
            }
          });
        }
      }
    })
  },


  //取消
  btn_cancel: function () {
    this.setData({ 'popup': 0 });
  },
  //提交
  formSubmit: function (e) {
    var formId = e.detail.formId;
    var user_info = app.globalData.userInfo;
    var postData = e.detail.value;
    var topicData = this.data.data;
    if (postData.content == "") {
      app.alert('回复不能为空哦');
      return false;
    }
    this.setData({ 'loading': 1 });
    this.setData({ 'disabled': 1 });
    var _this = this;
    //提交回答
    app.api("sns/comment/add", {
      to_comment_id: this.data.to_comment_id,
      pid: this.data.id,
      formId: formId,
      to_user_id: this.data.to_user_id,
      content: postData.content,
      type: this.data.commentType,
      method: 'POST',
    }, function (res) {
      _this.setData({ 'loading': 0 });
      _this.setData({ 'disabled': 0 });
      var title = "";
      if (res.code == 200) {
        title = "提交成功"
        _this.setData({ 'popup': 0 });
        var listData = _this.data.listData;
        var len = listData.length;
        res.data.delAct = 1;
        listData[len] = res.data;
        topicData.comment_count++;
        _this.setData({ listData: listData, data: topicData });
      } else {
        app.alert(res.msg);
      }
      wx.showToast({
        title: title,
        icon: 'success',
        duration: 2000
      })
      that.getTopic(1);
    })

    console.log(e);
  },
  //浏览图片
  previewImage: function (e) {
    var current = e.target.dataset.src
    wx.previewImage({
      current: current,
      urls: this.data.data.pics
    })
  },

  //滚到底部 
  scrolltolower: function (e) {
    this.getComments();
  },

  //下拉刷新
  onPullDownRefresh: function () {
    this.init(1);
  },
  onHide: function () {
    this.setData({ popup: 0, showShare: 0 });
    // this.setData({ showShare: 0 });
  },
  onShow: function () {
    var reload = app.getPageData("topic/detail", 'reload');
    if (reload == 1) {
      this.init(1);
    }
  },

  topic_del: function () {
    var id = this.data.data.id;
    wx.showModal({
      title: '删除',
      content: '确认删除吗？',
      success: function (res) {
        if (res.confirm) {
          app.api("sns/topic/del", { id: id }, function (res) {
            if (res.code != 200) {
              app.alert(res.msg);
              return false;
            }
            app.alert("操作成功");

            app.setPageData("group/index", { reload: 1 });
            app.setPageData("index/index", { reload: 1 });

   
            wx.navigateBack({
              delta: 1
            })
          })
        }
      }
    })
  },

  moreAct: function () {
    var that = this;
    var itemListArray = [];
    if (this.data.data.user_id == this.data.user_info.user_id) {
      itemListArray.push({ text: '编辑', act: function(){
        wx.navigateTo({
          url: '/pages/group/issue?topic_id=' + that.data.data.id,
        })
      } });
      itemListArray.push({
        text: '删除', act: that.topic_del
        
      });
    }

    //   if ((this.data.user_info.tenement_type.company || this.data.user_info.tenement_type.employee) && this.data.data.status<3 && this.data.data.pid ==91) {
    //   var text = '处理';
    //   if (this.data.data.status==1){
    //     text ='接受处理';
    //   }
    //   if (this.data.data.status == 2) {
    //     text = '处理完毕';
    //   }
    //   itemListArray.push({
    //     text: text, act: that.dealComment
    //   });
    // }
    
    itemListArray.push({
      text: '返回', act: function () {
        wx.navigateBack();
      }
    });

    var itemList = [];
    //console.log(itemListArray);
    for (var i in itemListArray) {
      itemList.push(itemListArray[i].text);
    }
    wx.showActionSheet({
      itemList: itemList,
      success: function (res) {
        console.log(itemListArray[res.tapIndex]);
        itemListArray[res.tapIndex]['act']();
        //  if (res.tapIndex == 0) {
        //    that.apply1();
        //  } else if (res.tapIndex == 1) {
        //    that.del();
        //  } else if (res.tapIndex == 2){
        //    that.go_ahead();
        //  } 
      },
      fail: function (res) {
        console.log(res.errMsg)
      }
    })
  },

  updateStatus: function () {
    var that = this;
    if (that.data.payloading == 1) {
      return;
    }
    that.data.payloading = 1;
    app.api("topic/updateStatus", {
      id: this.data.data.topic_join.id
    }, function (res) {
      if (res.code != 200) {
        app.alert(res.msg);
        that.data.payloading = 0;
        return false;
      }
      that.suc('操作成功');
    })
  },
 
  
  suc: function (tip = '加入成功') {
    app.alert(tip);
    this.init(1);
    app.setPageData("user/timeline", { reload: 1 });

  },
  


})
